#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> ret;
        for (int index_cur = 0; index_cur < nums.size() - 2 && nums[index_cur] <= 0; index_cur++)
        {
            if (index_cur && nums[index_cur] == nums[index_cur - 1])continue;
            int index_left = index_cur + 1, index_right = nums.size() - 1;
            int tgt_sum = -nums[index_cur];
            while (index_left < index_right)
            {
                int sum = nums[index_left] + nums[index_right];
                if (sum < tgt_sum)index_left++;
                else if (sum > tgt_sum)index_right--;
                else
                {
                    ret.push_back({ nums[index_cur],nums[index_left],nums[index_right] });
                    while (index_left < index_right && nums[index_left] == nums[index_left + 1]) index_left++;
                    while (index_left < index_right && nums[index_right] == nums[index_right - 1]) index_right--;
                    index_left++;
                    index_right--;
                }
            }
        }
        return ret;
    }
};